package com.txunda.zhuohired.fgt.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.ExpectJobBean;
import com.txunda.zhuohired.view.FlowLayout;
import com.txunda.zhuohired.view.TagAdapter;
import com.txunda.zhuohired.view.TagFlowLayout;

import java.util.ArrayList;
import java.util.Set;

/**
 * Created by admin on 2018/3/2.
 */

public class ExpectJobFragment extends BaseFgt {
    /**
     * 标题栏
     */
    @ViewInject(R.id.id_flowlayout)
    private TagFlowLayout id_flowlayout;

    TagAdapter selectlistAdapter;

    public static ExpectJobFragment newInstance(ArrayList<ExpectJobBean> list, ArrayList<ExpectJobBean> selectList, TagAdapter selectlistAdapter) {
        Bundle args = new Bundle();
        args.putSerializable("list", list);
        args.putSerializable("selectList", selectList);
        args.putSerializable("selectlistAdapter", selectlistAdapter);
        ExpectJobFragment fragment = new ExpectJobFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final LayoutInflater mInflater = LayoutInflater.from(getActivity());
        final ArrayList<ExpectJobBean> list = (ArrayList<ExpectJobBean>) getArguments().getSerializable("list");
        final ArrayList<ExpectJobBean> selectList = (ArrayList<ExpectJobBean>) getArguments().getSerializable("selectList");
        final TagAdapter selectlistAdapter = (TagAdapter) getArguments().getSerializable("selectlistAdapter");
        id_flowlayout.setAdapter(new TagAdapter<ExpectJobBean>(list) {

            @Override
            public View getView(FlowLayout parent, int position, ExpectJobBean s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tag_flow_layouttv,
                        id_flowlayout, false);
                tv.setText(s.getName());
                return tv;
            }
        });
        id_flowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
        {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent)
            {
           /*   if (selectList.contains(list.get(position))){
                  selectList.remove(list.get(position));
                  selectlistAdapter.notifyDataChanged();
              }else {
                  if (selectList.size()>= 5){
                      Toast.makeText(getContext(),"已选择5个",Toast.LENGTH_SHORT).show();
                      Set<Integer> selectedList = id_flowlayout.getSelectedList();
                      selectedList.remove(position);
                      id_flowlayout.getAdapter().setSelectedList(selectedList);
                  }else {
                      selectList.add(list.get(position));
                      selectlistAdapter.notifyDataChanged();
                  }
              }*/
                boolean isChecked = false;
                for (ExpectJobBean bean:selectList
                     ) {
                    if (bean.getId().equals(list.get(position).getId()))
                        isChecked = true;
                }
                if (!isChecked){
                    if (selectList.size()>= 5){
                        Toast.makeText(getContext(),"已选择5个",Toast.LENGTH_SHORT).show();
                    }else {
                        selectList.add(list.get(position));
                        selectlistAdapter.notifyDataChanged();
                    }
                }else {
                    Toast.makeText(getContext(),"已选中该职位",Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

    }

    @Override
    protected void immersionInit() {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_expect_job;
    }

    @Override
    protected void initialized() {

    }

    @Override
    protected void requestData() {

    }
}
